package com.mmdccj.yugu.mapper;

import com.mmdccj.yugu.dto.MusicDto;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.entity.Music;
import com.mmdccj.yugu.entity.Play;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: star_
 * @date: 2023-11-22 21:57
 */
@Mapper
public interface MusicMapper {

    // 查询歌单的信息
    @Select("SELECT " +
            "id, creator_id, introduce, " +
            "title, image, create_date " +
            "FROM music_list " +
            "WHERE id = #{musicListId}")
    MusicListDto getListInfoByMusicList(String musicListId);

    // 插入新歌
    @Insert("INSERT INTO music(title,introduce,duration,play_count,image,src,publish_date,style,format)" +
            " VALUES(#{title},#{introduce},#{duration},0,#{image},#{src},#{date},#{style},#{format})")
    void insertMusic(String title, String introduce, int duration, String image, String src, LocalDateTime date, String format, String style);

    @Select("SELECT music_id from music WHERE src=#{src}")
    String getMusicIdBySrc(String src);

    // 查询歌单中的所有音乐
    @Select("SELECT music.music_id, duration,title, play_count, src,image " +
            "FROM music_link_list link JOIN music " +
            "ON link.music_id = music.music_id " +
            "AND link.music_list_id = #{musicListId}")
    List<Music> getMusicByMusicList(String musicListId);

    //根据id查询音乐
    @Select("SELECT * FROM music WHERE music_id = #{musicId}")
    Music getMusicById(String musicId);

    // 根据userId查询某个歌手的50首曲目
    @Select("SELECT * FROM music_link_singer l join music m ON l.music_id = m.music_id WHERE l.user_id=#{userId}")
    List<Music> getSingerAllMusicById(String userId);

    // 更新播放次数
    @Update("UPDATE music SET play_count = play_count + 1 WHERE music_id = #{musicId}")
    void updateMusicPlays(String musicId);

    @Insert("INSERT INTO music_link_singer(music_id,user_id) VALUES(#{musicIdBySrc},#{userId})")
    void insertMusicLinkSinger(String userId, String musicIdBySrc);

    @Select("SELECT *,(SELECT list.title FROM music_list list join music_link_list link2 ON link2.music_list_id = list.id AND link2.music_id = m.music_id AND list.is_cd = TRUE) AS cdName  FROM music_link_singer link join music as m on m.music_id=link.music_id WHERE link.user_id=#{userId}")
    List<Music> getUserMusic(String userId);

    @Update("UPDATE music SET duration=#{duration},title=#{title},src=#{music},image=#{image},introduce=#{intro},format=#{format},style=#{style} WHERE music_id=#{musicId}")
    void updateMusic(String musicId, Integer duration, String title, String music, String image, String intro, String format, String style);

    /**
     * 获取用户上传的未被加入专辑的音乐列表
     *
     * @param userId 用户id
     * @return List<Music>
     */
    @Select("SELECT * " +
            "from music " +
            "WHERE music_id " +
            "    NOT IN (SELECT m.music_id " +
            "            FROM music_list list " +
            "                     JOIN music_link_list link " +
            "                     JOIN music m ON link.music_id = m.music_id AND link.music_list_id = list.id " +
            "            WHERE is_cd = TRUE) " +
            "  AND music_id IN (SELECT s_link.music_id " +
            "                   FROM music_link_singer s_link " +
            "                            JOIN user u ON s_link.user_id = u.id AND user_id = #{userID})")
    List<Music> getUserAllMusicWithoutCD(String userId);

    @Delete("DELETE  from music WHERE music_id=#{musicId} ")
    void delete(String musicId);

    @Delete("DELETE  from music_link_singer WHERE music_id = #{musicId} AND user_id=#{userId}")
    void deleteLinkSinger(String userId, String musicId);

    @Delete("DELETE  from music_link_list WHERE music_id = #{musicId}")
    void deleteLinkList(String musicId);

    List<MusicDto> getAllMusic();

    @Select("SELECT * FROM user_plays WHERE user_id=#{userId}")
    List<Play> getAllPlayNums(String userid);

    List<MusicDto> getMostLikeMusic();

    List<MusicDto> getNewMusic();
}
